home *** CD-ROM | disk | FTP | other *** search
- Path: cyberspace.com!loomis!icarus
- From: icarus@loomis (Tel Janin Aellinsar)
- Newsgroups: comp.lang.c
- Subject: Pointers to structures
- Date: 2 Jan 1996 21:59:00 GMT
- Organization: Chosen Suite #13, Shayol Ghul Resort and Health Spa
- Message-ID: <4cc9r4$m26@armitage.cyberspace.com>
- NNTP-Posting-Host: berkshire.net
- X-Newsreader: TIN [version 1.2 PL2]
-
- I'm having trouble using a pointer to a struct. The idea is to have a
- struct get filled by a function, which gets the address and everything
- via function(struct type*). Very straightforward. However, if I try to
- CHANGE anything in the struct, it just goes back when the function is
- over. So, let's pretend this imaginary structure is what I'm using:
-
- struct st1 {
- char *name;
- int yadda;
- };
-
- And the function is:
-
- fn1(struct st1 *st)
- {
- st = (struct st1*)malloc(sizeof(struct st1*));
- st->name = (char*)malloc(16);
- strcpy(st->name,"Test");
- st->yadda = 100;
- }
-
- At the last line of code in fn1, all the values are correct; st->name is
- "Test" and st->yadda is 100. As soon as it exits, however, st->name
- points to NULL and st->yadda is zero. The allocation of the structure in
- the first line of fn1 doesn't seem to matter; both ways, I get NULL
- pointers coming out of my ears.
-
- Anybody who can help out there? I'm not sure why it keeps bouncing
- back... there must be something I'm missing, but I don't see why it
- shouldn't work the way it does.
-
- E-mail replies, please... thanks!
-
- --
- ________________________________________________________________________
- Tel Janin Aellinsar http://www.crocker.com/~icarus
- McCoy Enterprises Corporation info@ICARUS.BERKSHIRE.NET
- Berserker Dragon, Knights of the Cosmos icarus@BERKSHIRE.NET
-